class mainScence extends eui.Component implements  eui.UIComponent {
	platte:eui.Group
	scroll:eui.Group
	masker:eui.Rect
	fisttouchpoint:egret.Point
	beginhere:boolean=false
	haveplatte:boolean=false
	plattebackgroundshape:egret.Shape
	backgroundcolor:number
	public constructor() {
		super();
	
	}

	protected partAdded(partName:string,instance:any):void
	{
		super.partAdded(partName,instance);
	}


	protected childrenCreated():void
	{
		super.childrenCreated();
	    this.scroll.mask=this.masker
        this.scroll.touchEnabled=false
		this.platte.touchEnabled=true
        this.beginlisten()
	    
		this.fillbackground(this.scroll)
		

	}
    public setplatte(x:number,y:number,color?:number)
	{

		if(!this.haveplatte)
		{
		 this.platte.width=x
		 this.platte.height=y
		 this.fillbackground(this.platte,color)
		
		}
		else  
	    {
		 if(this.platte.getChildIndex(this.plattebackgroundshape)!=-1)
		 this.platte.removeChild(this.plattebackgroundshape)
		 this.platte.width=x
		 this.platte.height=y
		 this.fillbackground(this.platte,color)
		}
		
		

	}
	public touchbegin(e:egret.TouchEvent)
	{
	   
	   if(e.target.objname!=undefined&&!CCollitionEditor.editor.state)
	   {
		   CMainEditor.editor.goal=e.target
		   CMainEditor.editor.changetreeselectByobjname(CMainEditor.editor.goal.objname)
		   CCollitionEditor.editor.goal=undefined;
		   if(CMainEditor.editor.goal.judge.length)CCollitionEditor.editor.goal=CMainEditor.editor.goal.judge[0]
		   else if(CMainEditor.editor.goal.unjudge.length)CCollitionEditor.editor.goal=CMainEditor.editor.goal.unjudge[0]
		   CMainEditor.editor.rightchangeTOcobject();
	   }
	  
	   else if(e.target.name.split("_")[0]=="itsajudgeshape"&&CCollitionEditor.editor.state)
	   {
		   CCollitionEditor.editor.goal=CBASE_MANAGE.getCBASE_MANAGE.getcollitionByshapename(e.target.name)
		   CCollitionEditor.editor.update();
		   CMainEditor.editor.goal=CBASE_MANAGE.getCBASE_MANAGE.getanyoneByname(CCollitionEditor.editor.goal.parent.objName)
		   CMainEditor.editor.changetreeselectBycollition(CCollitionEditor.editor.goal)
		    CMainEditor.editor.rightchangeTOcollition();
	   }
	   this.beginhere=true;
	   this.fisttouchpoint=new egret.Point(e.stageX,e.stageY)
	}
	public tonchend(e:egret.TouchEvent)
	{
		this.beginhere=false;
		this.platte.numChildren
	}
	public touchmove(e:egret.TouchEvent)
	{
		
		if(this.beginhere&&!CMainEditor.editor.isROTA)
		{
        let nowpoint=new egret.Point(e.stageX,e.stageY);
		let vector=this.fisttouchpoint.subtract(nowpoint);
		
		this.platte.x-=vector.x;
		this.platte.y-=vector.y;
	
		this.fisttouchpoint=nowpoint
	     CObjectEditor.editor.update()
		 CCollitionEditor.editor.update();
		}

	}
	public addchild(x:number,y:number)
	{
		if(!this.haveplatte)return;
		let localpoint=this.scroll.globalToLocal(x,y)
		if(localpoint.x<0||localpoint.y<0||localpoint.x>this.scroll.width||localpoint.y>this.scroll.height)return;
		
		let plattelocalpoint=this.platte.globalToLocal(x,y)
		
		let obj=CMainEditor.editor.goal
		
		this.platte.addChild(obj);
		obj.touchEnabled=true;
		obj.x=plattelocalpoint.x
		obj.y=plattelocalpoint.y
	     let change:boolean=false
			if(obj.x<0)
			{
				this.platte.width-=obj.x
				this.platte.x+=obj.x
				change=true
			}
		    if(obj.x+obj.width>this.platte.width)
			{
				this.platte.width+=(obj.x+obj.width)-this.platte.width
				change=true
			}
			if(obj.y<0)
			{
				this.platte.height-=obj.y
				this.platte.y+=obj.y
				change=true
			}
			if(obj.y+obj.height>this.platte.height)
			{
				this.platte.height+=(obj.y+obj.height)-this.platte.height
				change=true
			}
			if(change)
			{
				this.fillbackground(this.platte,656565)
			}
			CMainEditor.editor.addtoleft();
			CMainEditor.editor.collitionchangetoobject();
			CMainEditor.editor.changetreeselectByobjname(obj.objName);


	}
	private fillbackground(a:eui.Group,color?:number)
	{
		this.plattebackgroundshape=new egret.Shape
		if(color==undefined)
		{
			this.plattebackgroundshape.graphics.beginFill(0xF0F0F0,0.5);
			if(a==this.platte)
			this.backgroundcolor=0xF0F0F0;	
		}
		else
		{
			this.plattebackgroundshape.graphics.beginFill(color,1);
			if(a==this.platte)
		    this.backgroundcolor=color;	
		}
		this.plattebackgroundshape.graphics.drawRect(0,0,a.width,a.height)
		this.plattebackgroundshape.graphics.endFill()
		a.addChildAt(this.plattebackgroundshape,0)
		this.plattebackgroundshape.x=0,this.plattebackgroundshape.y=0
		if(a==this.platte)
		this.haveplatte=true;
		
		//this.plattebackgroundshape.graphics.
	
	}
	public stagexy(x:number,y:number):egret.Point
	{
		return this.platte.localToGlobal(x,y);
	}
	public plattexy(x:number,y:number):egret.Point
	{
        return this.platte.globalToLocal(x,y);
	}
	public getgoalAt(c:egret.DisplayObject):number
	{
		return this.platte.getChildIndex(c);
	}
	public changeGoalIndex(index:number)
	{
		let a=CMainEditor.editor.goal
		this.platte.setChildIndex(a,index);
	}
	public removeCURRENtarget()
	{
		this.platte.removeChild(CMainEditor.editor.goal);
	}
	public beginlisten()
	{
       	this.platte.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.touchmove,this);
		this.platte.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.touchbegin,this);
		this.platte.addEventListener(egret.TouchEvent.TOUCH_END,this.tonchend,this);
	}
	public stoplisten()
	{
		this.platte.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.touchmove,this);
		this.platte.removeEventListener(egret.TouchEvent.TOUCH_BEGIN,this.touchbegin,this);
		this.platte.removeEventListener(egret.TouchEvent.TOUCH_END,this.tonchend,this);
	}
	public clearALL()
	{
		this.platte.removeChildren();
		
		
	}
	public loadingALL(obj:CSPRITE)
    {
		this.platte.addChild(obj);
		obj.touchEnabled=true;
	}
	public getplattedata():number[]
	{
        let a:number[]=[]
		a.push(this.platte.width)
		a.push(this.platte.height)
		a.push(this.backgroundcolor)
		return a;
	}


	
}